package com.np.adapterdemo2;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.np.adapterdemo2.pojo.Blog;

import java.util.ArrayList;
import java.util.List;

public class ChoseBlogDialog extends Dialog {

    private static final String TAG = ChoseBlogDialog.class.getSimpleName();
    private ButtonClickedCallback mCallback;
    private BlogAdapter mAdapter;
    private List<Blog> mBlogList = new ArrayList<>();
    private RecyclerView mRecyclerView;

    protected ChoseBlogDialog(@NonNull Context context) {
        super(context);
    }

    protected ChoseBlogDialog(@NonNull Context context, int themeResId) {
        super(context, themeResId);
    }

    protected ChoseBlogDialog(@NonNull Context context, boolean cancelable, @Nullable OnCancelListener cancelListener) {
        super(context, cancelable, cancelListener);
    }

    @SuppressLint("NotifyDataSetChanged")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_chose_blog);

        mRecyclerView = findViewById(R.id.rv);

        // 模拟数据
        for (int i = 0; i < 50; i++) {
            Blog blog = new Blog();
            blog.setTitle("标题" + i);
            blog.setContent("内容" + i);
            mBlogList.add(blog);
        }

        mAdapter = new BlogAdapter();
        mRecyclerView.setAdapter(mAdapter);
        mRecyclerView.setLayoutManager(new LinearLayoutManager(getContext()));

        findViewById(R.id.tv_cancel).setOnClickListener(view -> {
            if (mCallback != null) {
                mCallback.onClicked(true, ChoseBlogDialog.this);
            }
        });

        findViewById(R.id.tv_confirm).setOnClickListener(view -> {
            if (mCallback != null) {
                mCallback.onClicked(false, ChoseBlogDialog.this);
            }
        });

        Window window = getWindow();
        WindowManager.LayoutParams params = window.getAttributes();
        params.gravity = Gravity.CENTER;
        window.setAttributes(params);
    }

    public static ChoseBlogDialog showDialog(Context c, ButtonClickedCallback callback) {
        ChoseBlogDialog dialog = new ChoseBlogDialog(c);
        dialog.show();
        dialog.setCallback(callback);
        dialog.setCancelable(false);
        return dialog;
    }

    public void setCallback(ButtonClickedCallback callback) {
        this.mCallback = callback;
    }

    public interface ButtonClickedCallback {
        void onClicked(boolean isLeft, ChoseBlogDialog dialog);
    }

    class BlogViewHolder extends RecyclerView.ViewHolder {
        TextView tvTitle;
        Button buttonSelect;

        public BlogViewHolder(@NonNull View v) {
            super(v);
            tvTitle = v.findViewById(R.id.tv_title);
            buttonSelect = v.findViewById(R.id.button_select);
        }
    }

    class BlogAdapter extends RecyclerView.Adapter<BlogViewHolder> {

        @NonNull
        @Override
        public BlogViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            View view = View.inflate(getContext(), R.layout.item_blog, null);
            BlogViewHolder viewHolder = new BlogViewHolder(view);
            return viewHolder;
        }

        @Override
        public void onBindViewHolder(@NonNull BlogViewHolder holder, int position) {
            Blog blog = mBlogList.get(position);
            if (holder.tvTitle != null) {
                holder.tvTitle.setText(blog.getTitle());
            }
        }

        @Override
        public int getItemCount() {
            return mBlogList.size();
        }
    }
}